package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.ReleaseProjectEntity;
import com.antler.smt.portal.mapper.ReleaseProjectEntityMapper;
import com.antler.smt.portal.mapper.ReleaseProjectInfoMapper;
import com.antler.smt.request.portal.ReleaseProjcetReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.ReleaseProjectService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 项目发布实现接口
 *
 * @author zhangyi
 * @date 2018/9/30 16:02
 */
@Service
public class ReleaseProjectServiceImpl implements ReleaseProjectService {

    @Autowired
    private ReleaseProjectEntityMapper releaseProjectEntityMapper;

    @Autowired
    private ReleaseProjectInfoMapper releaseProjectInfoMapper;


    /**
     * 发布项目实现
     *
     * @param r
     * @return
     */
    @Override
    public Result releaseProject(ReleaseProjcetReq r) {
        ReleaseProjectEntity rel= new ReleaseProjectEntity();
        rel.setAttachmentId(r.getAttachmentId());
        rel.setMemberId(r.getMemberId());
        rel.setProjcetIntroduction(r.getProjcetIntroduction());
        rel.setProjectName(r.getProjectName());
        rel.setTotalMoney(r.getTotalMoney());
        rel.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
        rel.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
        int i = releaseProjectEntityMapper.insertSelective(rel);
        return ReturnResultUtil.getResult("项目发布成功","项目发布失败",i);
    }

    /**
     * 查看当前的项目需求
     *
     * @param memberId
     * @return
     */
    @Override
    public Result getProject(Long memberId) {
        Result result = null;
        Long id = releaseProjectInfoMapper.selectIdByMemberId(memberId);
        ReleaseProjectEntity releaseProjectEntity = releaseProjectEntityMapper.selectByPrimaryKey(id);
        if (releaseProjectEntity != null){
            result = new Result(releaseProjectEntity);
        }
        return result;
    }

    /**
     * 修改当前的发布需求
     *
     * @param r
     * @return
     */
    @Override
    public Result editProject(ReleaseProjcetReq r) {
        ReleaseProjectEntity releaseProjectEntity = new ReleaseProjectEntity();
        releaseProjectEntity.setTotalMoney(r.getTotalMoney());
        releaseProjectEntity.setProjectName(r.getProjectName());
        releaseProjectEntity.setProjcetIntroduction(r.getProjcetIntroduction());
        releaseProjectEntity.setAttachmentId(r.getAttachmentId());
        Long id = releaseProjectInfoMapper.selectIdByMemberId(r.getMemberId());
        int i = releaseProjectEntityMapper.updateByPrimaryKeySelective(releaseProjectEntity);
        return ReturnResultUtil.getResult("修改需求成功！","修改需求失败",i);
    }

}
